import java.util.Random;

/**
 * 给定一个很长的数组 (长度 1000w), 通过随机数的方式生成 1-100 之间的整数.
 * 实现代码, 能够创建两个线程, 对这个数组的所有元素求和.
 * 其中线程1 计算偶数下标元素的和, 线程2 计算奇数下标元素的和.
 * 最终再汇总两个和, 进行相加
 * 记录程序的执行时间.
 */
public class demo1 {
    private  static final int array_length =  10000000;
   private static int[] longArray = new int[array_length];
   private static long oddSum = 0;
   private static long evenSum = 0;
    public static void main(String[] args) throws InterruptedException {
        Random random = new Random();
        for (int i = 0 ; i < longArray.length ; i++){
            longArray[i] = random.nextInt(100)+1;
        }
        long startTime = System.currentTimeMillis();

        Thread thread1 = new Thread(() -> {
            for (int i = 0 ; i < array_length; i += 2 ){
                evenSum += longArray[i];
            }
        });
        Thread thread2 = new Thread(() ->{
            for (int i = 1 ; i < array_length; i += 2 ){
                oddSum += longArray[i];
            }
        });
        thread1.start();
        thread2.start();
        thread1.join();
        thread2.join();
        long endTime = System.currentTimeMillis();
        System.out.println("count = "+(oddSum+evenSum));
        System.out.println("Execution time :" + (endTime - startTime) + "ms");



    }
}
